import Vue from 'vue'
import VueRouter from "vue-router";
// 引入vuex
import store from '@/store'

// 引入组件
import Layout from '../views/Layout'
import Find from '@/views/Find'
import History from '@/views/History'
import Star from '@/views/Star'
import VideoList from '@/views/VideoList'
// Find页面路由组件
import Recommend from '@/views/Find/Recommend'
import SongList from '@/views/Find/SongList'
import Ranking from '@/views/Find/Ranking'
import Singer from '@/views/Find/Singer'

// 歌单组件
import SongSheet from '@/views/SongSheet'
import SongSheetList from '@/views/SongSheet/SongSheetList'
import Comment from '@/views/SongSheet/Comment'
import Stars from '@/views/SongSheet/Stars'

// 搜索组件
import Search from '@/views/Search'
import SongRes from '@/views/Search/SongRes'
import SingerRes from '@/views/Search/SingerRes'
import VideoRes from '@/views/Search/VideoRes'
import SheetRes from '@/views/Search/SheetRes'
import AlbumRes from '@/views/Search/AlbumRes'
// 歌手详情组件
import Detail from '@/views/Detail'
import SingerSong from '@/views/Detail/SingerSong'
import SingerMv from '@/views/Detail/SingerMv'
import SingerInfo from '@/views/Detail/SingerInfo'
import AlikeSinger from '@/views/Detail/AlikeSinger'

// 引入play组件
import Play from '@/views/Play'

// 引入视频组件
import VideoRecom from '@/views/VideoList/VideoRecom'
import MvRecom from '@/views/VideoList/MvRecom'
import Watch from '@/views/Watch'
Vue.use(VueRouter)
//路由重复 报错解决办法
const originalPush = VueRouter.prototype.push
	VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err)
}

const routes = [
    {
        path:'/',
        redirect:'/find',
        component:Layout,
        children:[
            {
                path:'find',
                name:'find',
                component:Find,
                redirect:'find/recommend',
                children:[
                    {
                        path:'recommend',
                        name:'recommend',
                        component:Recommend
                    },
                    {
                        path:'songlist',
                        name:'songlist',
                        component:SongList
                    },
                    {
                        path:'ranking',
                        name:'ranking',
                        component:Ranking
                    },
                    {
                        path:'singer',
                        name:'singer',
                        component:Singer
                    },
                ]
            },
            {
                path:'star',
                name:'star',
                component:Star,
            },
            {
                path:'history',
                name:'history',
                component:History,
            },
            {
                path:'videolist',
                name:'videolist',
                component:VideoList,
                redirect:'videolist/video',
                children:[
                    {
                        path:'video',
                        name:'videorecom',
                        component:VideoRecom
                    },
                    {
                        path:'mv',
                        name:'mvrecom',
                        component:MvRecom
                    },
                ]
            },
            {
                path:'songsheet',
                name:'songsheet',
                component:SongSheet,
                redirect:'songsheet/songsheetlist',
                children:[
                    {
                        path:'songsheetlist',
                        name:'songsheetlist',
                        component:SongSheetList
                    },
                    {
                        path:'comment',
                        name:'comment',
                        component:Comment
                    },
                    {
                        path:'stars',
                        name:'stars',
                        component:Stars
                    }
                ]
            },
            {
                path:'search',
                name:'search',
                component:Search,
                redirect:'search/song',
                children:[
                    {
                        path:'song',
                        name:'songRes',
                        component:SongRes
                    },
                    {
                        path:'singer',
                        name:'singerRes',
                        component:SingerRes
                    },
                    {
                        path:'video',
                        name:'videoRes',
                        component:VideoRes
                    },
                    {
                        path:'sheet',
                        name:'sheetRes',
                        component:SheetRes
                    },
                    {
                        path:'album',
                        name:'albumRes',
                        component:AlbumRes
                    },
                ]
            },
            {
                path:'detail',
                name:'detail',
                component:Detail,
                redirect:'detail/singersong',
                beforeEnter: (to, from, next) => {
                    if(to.query.id){
                        next();
                    }else{
                        next('/find/recommend')
                    }
                },
                children:[
                    {
                        path:'singersong',
                        name:'singersong',
                        component:SingerSong
                    },
                    {
                        path:'singermv',
                        name:'singermv',
                        component:SingerMv
                    },
                    {
                        path:'singerinfo',
                        name:'singerinfo',
                        component:SingerInfo
                    },
                    {
                        path:'alikesinger',
                        name:'alikesinger',
                        component:AlikeSinger
                    },
                ]
            },
            {
                path:'play',
                name:'play',
                component:Play,
                beforeEnter: (to, from, next) => {
                    if(store.state.play.id){
                        next();
                    }else{
                        next('/find/recommend')
                    }
                },
            },
            {
                path:'watch',
                name:'watch',
                component:Watch,
                beforeEnter: (to, from, next) => {
                    if(to.query.id){
                        next();
                    }else{
                        next('/find/recommend')
                    }
                },
            }
        ]
    }
]

const router = new VueRouter({
    mode:'history',
    linkExactActiveClass:'tabactive',
    routes
})
// 每次切换页面时，滚回顶部
router.afterEach((to,from,nex)=>{
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
   })

//全局前置路由守卫--初始化的时候与每次路由切换之前调用
router.beforeEach((to,from,next)=>{
   if(to.name=='songsheetlist' || to.name=='search'){
        next();
   }
   if(to.name!=from.name){
    next();
   }
})
export default router